package com.system.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.system.domain.SysPrinterServerBo;
import com.system.service.SysprintergentSN;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;

import static com.system.service.PrinterServiceImpl.executeCommand;

@RequiredArgsConstructor
@Service
public class SysprintergetSNImpl implements SysprintergentSN {

    private final String filePath = "./sn.txt";

    @Override
    public SysPrinterServerBo getWindowsSN() throws IOException {

        // 获取电脑品牌
        String manufacturer = executeCommand("wmic csproduct get vendor").trim();
        // 获取电脑型号
        String model = executeCommand("wmic csproduct get name").trim();
        // 获取电脑序列号
        String serialNumber = executeCommand("wmic bios get serialnumber").trim();

        System.out.println("电脑品牌: " + manufacturer);
        System.out.println("电脑型号: " + model);
        System.out.println("电脑序列号: " + serialNumber);

        SysPrinterServerBo sysPrinterServerBo= new SysPrinterServerBo();
        sysPrinterServerBo.setServerSn(serialNumber);
        sysPrinterServerBo.setServerName("Printer."+serialNumber);
        sysPrinterServerBo.setServerModel(model);
        sysPrinterServerBo.setServerBrand(manufacturer);
        sysPrinterServerBo.setAllowed(false);
        //品牌
        if (StrUtil.isBlank(sysPrinterServerBo.getServerBrand())) {
            sysPrinterServerBo.setServerBrand("未知");
        }
        //型号
        if (StrUtil.isBlank(sysPrinterServerBo.getServerModel())) {
            sysPrinterServerBo.setServerModel("未知");
        }
        //序列号
        if (StrUtil.isBlank(sysPrinterServerBo.getServerSn())) {
            boolean exist = FileUtil.exist(filePath);
            if (!exist) {
                FileUtil.writeUtf8String(UUID.fastUUID().toString(true), filePath);
            }
            //读取sn.txt内的序列号,使用Hutool
            BufferedReader reader = FileUtil.getReader(filePath, "utf-8");
            sysPrinterServerBo.setServerSn(reader.readLine());
            sysPrinterServerBo.setServerName("Printer."+sysPrinterServerBo.getServerSn());
        }


        return sysPrinterServerBo;

    }
}
